import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class JerryReceive implements Runnable {
	DatagramSocket ds = null;
	public JerryReceive(DatagramSocket ds) {
		this.ds = ds;
	}
	@Override
	public void run() {
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		while (true) {
			try {
				ds.receive(dp);
				// 收到之后，然后从包里面获取所有的数据
				String id = dp.getAddress().getHostAddress();// 必须获得id 
				int port = dp.getPort();
				String str = new String(dp.getData(), 0, dp.getLength());
				if ("over".equalsIgnoreCase(str)) {
					System.out.println("对方离开了聊天室");
					break;
				}
				System.out.println("id=" + id + ",port" + port + ",接收到的内容----" + str);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}